<?php
/**
 * Created by PhpStorm.
 * User: 风哀伤
 * Date: 2020/3/23
 * Time: 17:02
 * @copyright: ©2019 浙江禾匠信息科技
 * @link: http://www.zjhejiang.com
 */

namespace app\jobs;


use yii\base\BaseObject;
use yii\queue\JobInterface;

class UpdateJob extends BaseObject implements JobInterface
{
    public function execute($queue)
    {
        \Yii::warning('更新队列');
        $id = \Yii::$app->queue3->delay(60)->push(new self());
        \Yii::warning('队列添加成功' . $id);
        $file = \Yii::$app->basePath . '/versions.php';
        if (file_exists($file) && function_exists('hash_file')) {
            $key = 'version_update';
            $list = \Yii::$app->cache->get($key);
            $fileHash = hash_file('sha256', $file);
            if ($list === $fileHash) {
                \Yii::warning('不需要更新3');
                return true;
            }
            \Yii::$app->cache->set($key, $fileHash);
            $versions = require \Yii::$app->basePath . '/versions.php';
        	$versions = array_values($versions);
            $f = $versions[count($versions) - 1];
            if ($f instanceof \Closure) {
                $f();
                \Yii::warning('执行成功');
            } else {
                throw new \Exception('未执行sql');
            }
        } else {
            throw new \Exception('文件不存在，或者hash_file方法被禁用');
        }
    }
}
